package com.lsu.app.config;

import com.lsu.app.config.target.RequestRoles;

import com.lsu.app.exception.RolePermissionException;
import com.lsu.app.service.LogService;
import com.lsu.app.tool.jwtUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * @author hjj
 * @date 2023/10/18 11:06
 */
@Component
@Aspect
public class AspectPermissionConfig { //aop权限校验代理类

    @Autowired
    private HttpServletRequest request;


    @Pointcut("@annotation(com.lsu.app.config.target.RequestRoles)")
    public void point(){}
    @Around("point()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        //管理员权限直接放行
        if(request.getSession().getAttribute("role").equals("admin")){
            return joinPoint.proceed();
        }
        //获取注解中的roles值
        String[] roles = ((MethodSignature) joinPoint.getSignature()).getMethod().getAnnotation(RequestRoles.class).value();
        //角色校验
        boolean contains = Arrays.asList(roles).contains(request.getSession().getAttribute("role").toString());
        if(contains){ //包含权限放行
            return joinPoint.proceed();
        }else{
            throw new RolePermissionException("权限不足");
        }
    }


}
